import re
import json
from .base_spider import BaseSpider


class XiechengSpider(BaseSpider):
    name = "xiecheng"
    datas_xpath = "//div[contains(@class,'comment_block')]"
    creator_xpath = "div/p[2]/span/text()"
    content_xpath = "div//div[@class='J_commentDetail']/text()"
    star_xpath = "div//span[@class='n']/text()"

    def get_create_date(self, data):
        create_data = data.xpath("div//span[@class='time']/text()").extract()[0]
        return re.findall("\d{4}-\d{2}-\d{2}", create_data)[0]


class XiechengPiaoSpider(BaseSpider):
    name = "xiecheng_piao"
    create_date_pattern = "%Y-%m-%d %H:%M"

    def get_datas(self, response):
        content = json.loads(response.text)
        return content['Comment']

    def get_creator(self, data):
        return data['uid']

    def get_create_date(self, data):
        return data['date']

    def get_content(self, data):
        return data['con']

    def get_star(self, data):
        return data['grade']

    def get_page_size(self, response):
        return json.loads(response.text)['PageCount']
